/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hoja.de.trabajo.pkg6;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author Roberto Juarez Fuentes
 */
public class HojaDeTrabajo6 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // Ingreso de cadena
        Scanner input = new Scanner(System.in).useDelimiter("");
        List frequency = new SinglyLinkedList();
        
      
        System.out.print("Ingrese cadena (Enter para codificar): ");
        
        while (input.hasNext()) {
            char c = input.next().charAt(0);
            if (c == '\n') // Si la ultima tecla ingresada es enter entonces sale del ciclo
                break;
            node1 query = new node1(c);
            node1 item = (node1)frequency.remove(query);
            if (item == null) {        
                frequency.addFirst(query);
            } else {                 
                item.frequency++;
                frequency.addFirst(item);
            }
        }

        Iterator ti = frequency.iterator();
        PriorityQueue trees = new Heap();
        
        while (ti.hasNext()) {
            trees.add(new huffmanTree((node1)ti.next()));
        }

        while (trees.size() > 1) {
            huffmanTree smallest = (huffmanTree)trees.remove();
            huffmanTree small = (huffmanTree)trees.remove();
            trees.add(new huffmanTree(smallest,small));
        }
        
        huffmanTree huffman = (huffmanTree)trees.remove();
        System.out.println("\n");
        huffman.print();
        System.out.print("Ingrese codigo a decodificar (Enter para decodificar): ");
        while (input.hasNext()) {
            char c = input.next().charAt(0);
            if (c == '\n') // Si la ultima tecla ingresada es enter entonces sale del ciclo
                break;
            
        }
    }
}
